﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using __ts.Core.Configuration;

namespace CCU.Update
{
    public class Packages : Dictionary<string, PackageInfo>
    {
        /// <summary>
        /// 
        /// </summary>
        public string errorInfo { get; private set; }
        public string url { get; private set; }
        public int total;
        public int pageNum { get; private set; }
        public int pageSize { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public Packages()
        {
            loadAndFill(null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        public Packages(int pageNum, int pageSize)
        {
            loadAndFill(null, pageNum, pageSize);
        }
        /// <summary>
        /// 
        /// </summary>
        public Packages(string url, int pageNum, int pageSize)
        {
            loadAndFill(url, pageNum, pageSize);
        }
        /// <summary>
        /// 
        /// </summary>
        private void loadAndFill(string url, int pageNum = 1, int pageSize = 15)
        {
            if (string.IsNullOrEmpty(url))
                this.url = ConfigManager.GlobalAppSettings["PackageStore"];
            else
                this.url = url;
            this.url = this.url.Trim(new char[] { '/' });
            this.pageNum = pageNum;
            this.pageSize = pageSize;

            this.url = string.Format("{0}/Default.aspx?apiKey=14245e110457a93b2c6b091e7cf842577c9c1fa543364141374535384534&p={1}&ps={2}",
                this.url,
                this.pageNum,
                this.pageSize);

            XmlDocument doc = new XmlDocument();
            Stream st = null;

            try
            {
                HttpWebRequest res = null;
                HttpWebResponse req = null;
                res = WebRequest.Create(this.url) as HttpWebRequest;
                res.Timeout = 1000 * 20;
                res.ReadWriteTimeout = 1000 * 20;
                req = res.GetResponse() as HttpWebResponse;
                st = req.GetResponseStream();
                doc.Load(st);
            }
            catch (Exception ex)
            {
                errorInfo = ex.Message;
            }

            if (doc != null && doc.HasChildNodes)
            {
                total = !int.TryParse(doc.SelectSingleNode("packageList").Attributes["total"].InnerText, out total) ? 0 : total;
                foreach (XmlNode node in doc.SelectNodes("packageList/package"))
                {
                    try
                    {
                        this.Add(node["id"].InnerText.Trim(), new PackageInfo()
                        {
                            id = node["id"].InnerText.Trim(),
                            version = node["version"].InnerText.Trim(),
                            description = node["description"].InnerText.Trim(),
                            authors = node["authors"].InnerText.Trim(),
                            size = node["size"].InnerText.Trim(),
                            md5 = node["md5"].InnerText.Trim(),
                            publishTime = node["publishTime"].InnerText.Trim()
                        });
                    }
                    catch { }
                }
            }

        }
    }
}
